/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.os.Handler;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilLog;
import com.ouertech.android.agm.lib.base.utils.UtilMD5;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseFullActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Image;
import com.simon.mengkou.data.bean.base.Niudan;
import com.simon.mengkou.data.bean.base.NiudanSKU;
import com.simon.mengkou.data.bean.base.Order;
import com.simon.mengkou.data.bean.base.PhoneAccessToken;
import com.simon.mengkou.data.bean.base.QQAccessToken;
import com.simon.mengkou.data.bean.base.SinaAccessToken;
import com.simon.mengkou.data.bean.base.WeixinAccessToken;
import com.simon.mengkou.data.bean.req.LoginReq;
import com.simon.mengkou.data.cache.DataCacheImpl;
import com.simon.mengkou.data.enums.EAccountType;
import com.simon.mengkou.data.enums.EPayResult;
import com.simon.mengkou.future.base.OuerException;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.utils.UtilOuer;

import java.util.ArrayList;
import java.util.List;


/**
 * @author : Zhenshui.Xia
 * @date : 2014年8月12日 
 * @desc : 闪屏页
 */
public class SplashActivity extends BaseFullActivity {
	//普通闪屏模式延时
	public static final int SPLASH_DELAY_NORMAL	= 3000;
	private long mTime;

	@Override
	protected void initLayout() {
		 setContentView(R.layout.activity_splash);
	}

	@Override
	protected void initViews() {
		 //简单的、非动态的闪屏
		if(OuerApplication.mUser == null) {
			execNormalSplash(SPLASH_DELAY_NORMAL, true);
		} else {//自动登录
			autoLogin();
		}

		//动态闪屏
       // execSplash();

        //初始化插件
        //todo...
	}

	/**
	 * 普通延时闪屏
	 */
	private void execNormalSplash(int delay, final boolean isLogin) {
		//延时自动进入主界面
		AgnettyFuture future = OuerApplication.mOuerFuture
        		.delaySplash(delay, new OuerFutureListener(this){

					@Override
					public void onComplete(AgnettyResult result) {
						super.onComplete(result);
						if(isLogin) {//打开登录界面
							OuerDispatcher.presentLoginActivity(SplashActivity.this);
						} else {//打开主界面
							OuerDispatcher.presentMainTabActivity(SplashActivity.this);
						}

						finish();
					}

        		});

        attachDestroyFutures(future);
	}


	/**
	 * 自动登录
	 * @param
	 */
	private void autoLogin() {
		LoginReq req = new LoginReq();
		int type = OuerApplication.mUser.getType();
		req.setType(type);

		if(EAccountType.QQ == type) {//qq自动登录
			QQAccessToken token = QQAccessToken.readAccessToken(this);
			req.setAuthToken(token.getAccessToken());
			req.setExpiredTime(token.getExpiresTime());
			req.setOpenId(token.getOpenId());
		} else if(EAccountType.SINA == type) {//新浪自动登录
			SinaAccessToken token = SinaAccessToken.readAccessToken(this);
			req.setAuthToken(token.getAccessToken());
			req.setRefreshToken(token.getRefreshToken());
			req.setExpiredTime(token.getExpiresTime());
			req.setUid(token.getUid());
		} else if(EAccountType.WECHAT == type) {//微信自动登录
			WeixinAccessToken token = WeixinAccessToken.readAccessToken(this);
			req.setType(EAccountType.WECHAT);
			req.setAuthToken(token.getAccessToken());
			req.setRefreshToken(token.getRefreshToken());
			req.setExpiredTime(token.getExpiresTime());
			req.setOpenId(token.getOpenId());
		} else if(EAccountType.PHONE == type) {//手机号码自动登录
			PhoneAccessToken token = PhoneAccessToken.readAccessToken(this);
			req.setPhone(token.getPhone());
			req.setPassWord(token.getPassword());
		}

		AgnettyFuture future = OuerApplication.mOuerFuture.login(req,
				new OuerFutureListener(this){

					@Override
					public void onStart(AgnettyResult result) {
						super.onStart(result);
						mTime = System.currentTimeMillis();
					}

					@Override
					public void onComplete(AgnettyResult result) {
						super.onComplete(result);
						//登录成功跳到主界面
						long costTime = System.currentTimeMillis() - mTime;
						int delay = SPLASH_DELAY_NORMAL - costTime> 0
								? (int) (SPLASH_DELAY_NORMAL - costTime) : 0;
						execNormalSplash(delay, false);
					}

					@Override
					public void onException(AgnettyResult result) {
						super.onException(result);
						//登录失败跳到登录界面
						long costTime = System.currentTimeMillis() - mTime;
						int delay = SPLASH_DELAY_NORMAL - costTime> 0
								? (int) (SPLASH_DELAY_NORMAL - costTime) : 0;
						execNormalSplash(delay, true);
					}

					@Override
					public void onNetUnavaiable(AgnettyResult result) {
						//网络异常跳到登录界面
						execNormalSplash(SPLASH_DELAY_NORMAL, true);
					}

				});

		//添加到销毁队列
		attachDestroyFutures(future);
	}

}

